/*
 * DeCompressZip.cpp
 *
 *  Created on: 2009-1-22
 *      Author: hequn
 */

#include "DeCompressZip.h"
#include <BAUTILS.H>


CDeCompressZip::~CDeCompressZip()
{
	Cancel();
	iTimer.Close();
	if(m_pDirectPath)
	{
		delete m_pDirectPath;
		m_pDirectPath = NULL;
	}
	
	if(m_pFilesOfZip)
	{
		delete m_pFilesOfZip;
		m_pFilesOfZip = NULL;
	}
	
	if(m_pZipfile)
	{
		delete m_pZipfile;
		m_pZipfile = NULL;
	}	
/*	if(m_Periodic)
	{
		delete m_Periodic;
		m_Periodic = NULL;
	}*/
	
	
	iFs.Close();
}

CDeCompressZip* CDeCompressZip::NewL()
{
	CDeCompressZip* self =  CDeCompressZip::NewLC();
	CleanupStack::Pop();
	return self;
}

CDeCompressZip* CDeCompressZip::NewLC()
{
	CDeCompressZip* self = new (ELeave) CDeCompressZip();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CDeCompressZip::CDeCompressZip() : CActive(EPriorityStandard)
{
}

void CDeCompressZip::ConstructL()
{
	User::LeaveIfError(iFs.Connect());
	User::LeaveIfError(iTimer.CreateLocal());
	CActiveScheduler::Add(this);
	//m_Periodic = CPeriodic::NewL(CActive::EPriorityIdle);
}

void CDeCompressZip::WriteFileToSave(const TDesC& afilenZipame, const TDesC& aPath, TBool aRemoveSource)
{
	Cancel();
	m_sourceFile.Copy(afilenZipame);
	m_isRemoveSourceFile = aRemoveSource;
	if(m_pZipfile)
		delete m_pZipfile;
	m_pZipfile = CZipFile::NewL(iFs,afilenZipame);
	
	//提取压缩文件中的总文件成员
	m_pFilesOfZip = m_pZipfile->GetMembersL();
	BaflUtils::EnsurePathExistsL(iFs,aPath);
	m_pDirectPath = HBufC::New(aPath.Length());
	m_pDirectPath->Des().Copy(aPath);
	
	//if(iStatus = KRequestPending)
	iTimer.After(iStatus,1000);
	SetActive();
	//m_Periodic->Start(0,100000,TCallBack(CDeCompressZip::WriteCallback,this));	
}

//TInt CDeCompressZip::WriteCallback(TAny* pInst)
void CDeCompressZip::RunL()
{
	//CDeCompressZip* pThis = (CDeCompressZip*)pInst;
	CZipFileMember* pMemberfile;
	
	if((pMemberfile = m_pFilesOfZip->NextL()) != 0)
	{
		WriteFileCyclingL(pMemberfile);
		//delete pMemberfile;
		
		iTimer.After(iStatus,1000);
		SetActive();
	}
	else
	{
		m_pZipfile->Close();
		Cancel();
		//删除原压缩包
		if(m_isRemoveSourceFile)
		{
			iFs.Delete(m_sourceFile);
		}
	}
}

void CDeCompressZip::DoCancel()
{
	FinishedWriteAndDestroyObj();
}

void CDeCompressZip::WriteFileCyclingL(CZipFileMember* pMember)
{
	
	TBuf16<100> buf_savefilename(m_pDirectPath->Des());
	buf_savefilename.Append(*pMember->Name());
	BaflUtils::EnsurePathExistsL(iFs,buf_savefilename);
	
	RFile rfile;
	if(rfile.Replace(iFs, buf_savefilename, EFileWrite) != KErrNone)
	{
		rfile.Close();//如果是目录文件（例如：_L("temp\\")）则返回
		return;
	}

	//提取压缩文件列表中第pMember文件的文件流
	RZipFileMemberReaderStream* stream;
	m_pZipfile->GetInputStreamL(pMember, stream);
	CleanupStack::PushL(stream);

	//读取pMember文件的内容并保存在data_buffer中
	HBufC8* data_buffer;
	data_buffer = HBufC8::NewLC(pMember->UncompressedSize());
	TPtr8 bufferPtr(data_buffer->Des());
	User::LeaveIfError(stream->Read(bufferPtr,pMember->UncompressedSize()));

	User::LeaveIfError(rfile.Write(*data_buffer));

	rfile.Close();
	CleanupStack::PopAndDestroy(data_buffer);
	CleanupStack::PopAndDestroy(stream);
		
}

void CDeCompressZip::FinishedWriteAndDestroyObj()
{
	//m_Periodic->Cancel();
	
	if(m_pDirectPath)
	{
		delete m_pDirectPath;
		m_pDirectPath = NULL;
	}
	/*
	if(m_pFilesOfZip)
	{
		delete m_pFilesOfZip;
		m_pFilesOfZip = NULL;
	}
	*/
	if(m_pZipfile)
	{
		delete m_pZipfile;
		m_pZipfile = NULL;
	}	
	
}
